// 1安装

// 2导入
import Vue from 'vue'
import VueRouter from 'vue-router'
import Nprogress from 'nprogress'
// css
import 'nprogress/nprogress.css'
// 导入路由文件
import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'

import data from '@/views/layout/child/data.vue'
import user from '@/views/layout/child/user.vue'
import question from '@/views/layout/child/question.vue'
import enterprise from '@/views/layout/child/enterprise.vue'
import subject from '@/views/layout/child/subject.vue'

// 防止多次点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
// 3注册
Vue.use(VueRouter)


// 4实例化
const router = new VueRouter({
    routes: [{
            path: '/login',
            component: login,
            meta: {
                title: "登录页"
            }
        },
        {
            path: '/',
            redirect: '/login',
            meta: {
                title: "登录页"
            }
        },
        {
            path: '/layout/',
            component: layout,
            // 默认打开----路由重定向
            redirect: '/layout/data',
            children: [{
                    // 数据概览
                    path: '/layout/data',
                    component: data,
                    meta: {
                        title: "数据概览"
                    }
                }, {
                    // 用户题库
                    path: '/layout/user',
                    component: user,
                    meta: {
                        title: "用户题库"
                    }
                }, {
                    // 题库列表
                    path: '/layout/question',
                    component: question,
                    meta: {
                        title: "题库列表"
                    }
                }, {
                    // 企业列表
                    path: '/layout/enterprise',
                    component: enterprise,
                    meta: {
                        title: "企业列表"
                    }
                }, {
                    // 学科列表
                    path: '/layout/subject',
                    component: subject,
                    meta: {
                        title: "学科列表"
                    }
                },


            ]
        },
    ]
})
// 导航守卫
// to、from----获取当前的路由信息--this.$route
// 前置守卫
router.beforeEach((to, from, next) => {
    window.console.log(to);
    window.console.log(from);
    Nprogress.start()
    next() //--允许通过
})
// 1配置路由的路由元meta信息，title(可以改，下面要对应)标题
// 2在后置导航守卫加上标题修改操作
// 后置守卫--已经进到路由
router.afterEach((to, from) => {
    window.console.log(to);
    window.console.log(from);
    document.title = to.meta.title
    Nprogress.done()
    Nprogress.configure({
        // 不显示转动的小圆圈
        showSpinner: false
    });
})
// 暴露出去
export default router
// 5注入
// 6路由出口